#include "main.h"
#include "system.h"

extern	ReceDataInfor*gNB_ReceData;

void LoRa_Uart_Send(const char *str, u32 size)
{
	printf("S--->>[%d]---%s\n",size,str);
	HAL_UART_Transmit(&NBUart, (uint8_t *)str, size, 200);	
	
}


void LoRo_Uart_CleanData(void)
{
	gNB_ReceData->len=0;
	memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
}

#if 0
static  u8 SumCheck(u8 *p, u8 length)
{
	u16 sum = 0;
	u8 *point = p;
	u8 i = 0;
	u16 crc = 0;
	for (i = 0; i < length - 2; i++) 
	{
		sum += (*(point + i));
	}
	crc = (((u16) * (p + length - 2)) << 8) | ((u16) * (p + length - 1));
	if (sum == crc) 
	{
		return 1;
	}
	else 
	{
		printf("crc err\n");
		return 0;
	}
}
static u16 SumCalculate(u8 *p, u8 length)
{
	u16 sum = 0;
	u8 *point = p;
	u8 i = 0;
	for (i = 0; i < length - 2; i++) 
	{
		sum += (*(point + i));
	}
	return sum;
}
#endif

uint8 ConvertStringToHex(uint8 *str)
{
	uint8 hightbyte, lowbyte;
	if (str[0] >= 'a')
	{
		hightbyte = (str[0] - 'a' + 10) * 16;
	}
	else
	{
		hightbyte = (str[0] - '0') * 16;
	}
	if (str[1] >= 'a')
	{
		lowbyte = str[1] - 'a' + 10;
	}
	else
	{
		lowbyte = str[1] - '0';
	}
	return hightbyte + lowbyte;
}

u32 Valid_Data_Len_Max = 101;
static void LoRaCtrlIOInit(void)
{

	
}


void LoraHardwareReset(void)
{
	
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin   = NB_PW_EN_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull  = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(NB_PW_EN_PORT, &GPIO_InitStruct);
	NB_PW_Disable;	
	GPIO_InitStruct.Pin 	= 	BC26_TX_Pin|BC26_RX_Pin;
	GPIO_InitStruct.Mode 	= 	GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Speed 	= 	GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(BC26_UART_Port,&GPIO_InitStruct);
	BC26_TXPIN_Low;
	BC26_RXPIN_Low;
	NB_Delay(2000);
	NB_PW_Enable;

    GPIO_InitStruct.Pin = BC26_TX_Pin | BC26_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
    HAL_GPIO_Init(BC26_UART_Port, &GPIO_InitStruct);	
}

void LoRaInit(void)
{
	LoraHardwareReset();


	
	return;
#if 0	
	int initState;

	initState = 1; 
	while (initState)
	{
		
		switch (initState)
		{
		case 1:				  
			
			NBLORA_UART_Init();
			initState = 2;
			NB_Delay(10);
			break;
		case 2: 
			LoRo_Uart_CleanData();
			/*******************************************************
			ģ���⵽�½���
			�󱻻��ѣ������������߻�����
			ָ�ģ�齫�ȴ� 10s ���Զ�����
			����
			********************************************************/

			
			
			
			

			/*******************************************************
			��ģ�鷢������ǰ�����ɸߵ�ƽ���ͣ�
			�ȴ�Լ 5ms �ٽ������ߣ��������ű���
			Ϊ�͵�ƽ����ģ��һֱ��������״̬��
			����������
			******************************************************/			
			initState = 4;
			break;

		case 4: 

			if (LoRaSleepMode(FALSE) == TRUE)
			{
				initState = 5;
				if(flag==0)
				{
					return SUCCESS;
				}			
			}
			else
			{
				initState = 2; 
				break;
			}
			LoRo_Uart_CleanData();
			LoRa_Uart_Send("AT+DEVEUI?\r\n", 12);
			
			
			break;

		case 5: 
			if (Set_Lora_Join() == SUCCESS)
			{
				printf("joined net\n");
				initState = 0;
				return SUCCESS;
			}
			else
			{
				initState = 1;
				LoRaModeReset();
				printf("join net failed\n");
			}
			return	ERROR;
		}
	}
	return	ERROR;
#endif	
}


/*
*********************************************************************************************************
*	�� �� ��: asciitohex
*	����˵��: �����ֽڵ�ascii��ϳ�1��16��������
*	�� �� ֵ: 16��������
*   ��    �ߣ�����
*   ��    �ڣ�2019-02-22
*********************************************************************************************************/
char asciitohex(char high_ascii, char low_ascii)
{
	char temp_high;
	char temp_low;
	if (high_ascii <= 0x39)
		temp_high = high_ascii - 0x30;
	else
		temp_high = high_ascii - 0x57;

	if (low_ascii <= 0x39)
		temp_low = low_ascii - 0x30;
	else
		temp_low = low_ascii - 0x57;

	temp_high = temp_high << 4 | temp_low;

	return temp_high;
}

/*
*********************************************************************************************************
*	�� �� ��: lowhextoascii
*	����˵��: 16���Ƶĵ��ֽڵ�4bitת����ASCII��
*	�� �� ֵ: ASCII��
*   ��    �ߣ�����
*   ��    �ڣ�2019-02-22
*********************************************************************************************************/
char lowhextoascii(char hexdata)
{
	char temp;
	temp = hexdata & 0x0f;
	if (temp < 10)
		temp = temp + 0x30;
	else
		temp = temp + 0x37;
	return temp;
}

/*
*********************************************************************************************************
*	�� �� ��: Lora_Tx_Data
*	����˵��: ��232/485�յ���16�������ݣ�ת����ASCII�룬��lora����ȥ��������̫������ض�
*	�� �� ֵ: ��
*   ��    �ߣ�����
*   ��    �ڣ�2019-02-22
*********************************************************************************************************/




void Lora_Tx_Data(char *str, u32 length)
{
	u32 i;
	u32 Valid_Data_Len;
	
	uint8 buf[100] = {0};
	buf[0] = 'A';
	buf[1] = 'T';
	buf[2] = '+';
	buf[3] = 'S';
	buf[4] = 'E';
	buf[5] = 'N';
	buf[6] = 'D';
	buf[7] = '=';
	buf[8] = '1';
	buf[9] = ',';
	buf[10] = '4';
	buf[11] = ',';
	if (length <= Valid_Data_Len_Max)
		Valid_Data_Len = length;
	else
		Valid_Data_Len = Valid_Data_Len_Max;

	for (i = 0; i < Valid_Data_Len; i++)
	{
		buf[2 * i + 13] = lowhextoascii(str[i]);
		buf[2 * i + 12] = lowhextoascii(str[i] >> 4);
	}
	buf[2 * i + 12] = '\r';
	buf[2 * i + 13] = '\n';
	LoRo_Uart_CleanData();
	LoRa_Uart_Send(buf, (2 * i + 14));
}
